package edu.vital.energy.longevity.ch11.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.vital.energy.longevity.ch11.dao.ArticleMapper;
import edu.vital.energy.longevity.ch11.dao.CategoryMapper;
import edu.vital.energy.longevity.ch11.model.domain.Category;
import edu.vital.energy.longevity.ch11.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleMapper articleMapper; // To update article counts or handle related articles

    @Override
    public void addCategory(String name) {
        Category category = new Category();
        category.setName(name);
        categoryMapper.insertCategory(category);
    }

    @Override
    public void deleteCategory(Integer id) {
// 补全删除目录的代码
    }

    @Override
    public void updateCategory(Category category) {

// 补全更新录的代码
    }

    @Override
    public Category getCategoryById(Integer id) {

        return null;
    }

    @Override
    public PageInfo<Category> getAllCategories(int page, int count) {
        PageHelper.startPage(page, count);

        List<Category> categories =  null ;
         for (Category category : categories) {
            category.setCount(articleMapper.countArticlesByCategories(category.getName()));
        }
        return new PageInfo<>(categories);
    }

    @Override
    public List<Category> getAllCategories() {
        List<Category> categories = categoryMapper.selectAllCategories();
        for (Category category : categories) {
            category.setCount(articleMapper.countArticlesByCategories(category.getName()));
        }
        return categories;
    }

    @Override
    public List<String> getAllCategoryNames() {
        return categoryMapper.selectAllCategoryNames();
    }
}